#import "OmLoginVC.h"
#import "OmRegisterVC.h"
#import "OmForgetPasswordVC.h"
#import "RootWebViewController.h"
@interface OmLoginVC ()
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *accountField;
@property (nonatomic, strong) UIView *accountFieldBottomLine;
@property (nonatomic, strong) UIImageView *accountFieldIcon;
@property (nonatomic, strong) UITextField *passwordField;
@property (nonatomic, strong) UIView *passwordFieldBottomLine;
@property (nonatomic, strong) UIImageView *passwordFieldIcon;
@property (nonatomic, strong) UIButton *forgetPasswordButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *protocolButton;
@end
@implementation OmLoginVC
#pragma mark - Event
- (void)onForgetPasswordButtonTap {
    NSLog(@"忘记密码");
    OmForgetPasswordVC *vc = [[OmForgetPasswordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onRegisterButtonTap {
    NSLog(@"账号注册");
    OmRegisterVC *vc = [[OmRegisterVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onLoginButtonTap {
    NSLog(@"登录");
    if ([self isRegisterInformationCorrect]) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"name"] = self.accountField.text;
        parameters[@"pass_word"] = self.passwordField.text;
        [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"登录中")];
        [userManager login:OmUserLoginTypeNormal params:parameters completion:^(BOOL success, NSString *des) {
            if(success) {
                [[HUDHelper sharedInstance] syncStopLoading];
                KPostNotification(KNotificationLoginStateChange, @YES);
            }
        }];
    }
}
- (void)onProtocolButtonTap {
    NSLog(@"协议");
    NSString *url = [URL_main stringByAppendingString:@"/admin/public/portal/article/index/id/7.html"];
    RootWebViewController *webView = [[RootWebViewController alloc] init];
    webView.url = url;
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
#pragma mark - Method
- (BOOL)isRegisterInformationCorrect {
    if (self.accountField.text.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请输入用户名")];
        return NO;
    }
    if (self.passwordField.text.length <= 0) {
        [MBProgressHUD showTipMessageInView:ASLocalizedString(@"请输入密码")];
        return NO;
    }
    return YES;
}
#pragma mark - ConstructionScript
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}
- (void)configSelf {
    [self setHiddenForTabBar:YES navigationBar:YES];
}
- (void)allocateComponents {
    self.backgroundImageView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"OmLoginBackground")].fp_contentMode(UIViewContentModeScaleAspectFill);
    [self.view addSubview:self.backgroundImageView];
    self.titleLabel = [[UILabel alloc] init].fp_text(ASLocalizedString(@"登录")).fp_font(BoldRPXFont(31));
    [self.view addSubview:self.titleLabel];
    self.accountField = [[UITextField alloc] init];
    [self.accountField setFont:RPXFont(15)];
    self.accountField.placeholder = ASLocalizedString(@"请输入账号");
    [self.view addSubview:self.accountField];
    self.accountFieldBottomLine = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#D9D9D9"));
    [self.view addSubview:self.accountFieldBottomLine];
    self.accountFieldIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"OmAccountFieldIconForLogin")];
    [self.view addSubview:self.accountFieldIcon];
    self.passwordField = [[UITextField alloc] init];
    [self.passwordField setFont:RPXFont(15)];
    self.passwordField.placeholder = ASLocalizedString(@"请输入密码");
    [self.view addSubview:self.passwordField];
    self.passwordFieldBottomLine = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#D9D9D9"));
    [self.view addSubview:self.passwordFieldBottomLine];
    self.passwordFieldIcon = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"OmPasswordFieldIconForLogin")];
    [self.view addSubview:self.passwordFieldIcon];
    self.forgetPasswordButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onForgetPasswordButtonTap), UIControlEventTouchUpInside);
    [self.forgetPasswordButton setTitle:ASLocalizedString(@"忘记密码") forState:UIControlStateNormal];
    [self.forgetPasswordButton setTitleColor:HexadecimalColor(@"#999999") forState:UIControlStateNormal];
    self.forgetPasswordButton.titleLabel.font = RPXFont(13);
    [self.view addSubview:self.forgetPasswordButton];
    self.registerButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onRegisterButtonTap), UIControlEventTouchUpInside);
    [self.registerButton setTitle:ASLocalizedString(@"账号注册") forState:UIControlStateNormal];
    [self.registerButton setTitleColor:HexadecimalColor(@"#999999") forState:UIControlStateNormal];
    self.registerButton.titleLabel.font = RPXFont(13);
    [self.view addSubview:self.registerButton];
    self.loginButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onLoginButtonTap), UIControlEventTouchUpInside);
    [self.loginButton setBackgroundImage:IMAGE_NAMED(@"OmLoginButton") forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    self.protocolButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onProtocolButtonTap), UIControlEventTouchUpInside);
    [self.protocolButton setTitle:ASLocalizedString(@"登录既代表您同意最终用户许可协议. 点击查看更多.") forState:UIControlStateNormal];
    [self.protocolButton setTitleColor:HexadecimalColor(@"#999999") forState:UIControlStateNormal];
    self.protocolButton.titleLabel.font = RPXFont(11);
    [self.view addSubview:self.protocolButton];
}
- (void)layoutComponents {
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(136));
        make.left.equalTo(self.view).offset(RPX(29));
    }];
    [self.accountField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(253));
        make.left.equalTo(self.view).offset(RPX(30));
        make.width.equalTo(@(RPX(230)));
    }];
    [self.accountFieldBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(281));
        make.left.equalTo(self.view).offset(RPX(20));
        make.width.equalTo(@(RPX(257)));
        make.height.equalTo(@(1));
    }];
    [self.accountFieldIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(19)));
        make.height.equalTo(@(RPX(22)));
        make.right.equalTo(self.accountFieldBottomLine);
        make.centerY.equalTo(self.accountField);
    }];
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(312));
        make.left.equalTo(self.view).offset(RPX(30));
        make.width.equalTo(@(RPX(230)));
    }];
    [self.passwordFieldBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(340));
        make.left.equalTo(self.view).offset(RPX(20));
        make.width.equalTo(@(RPX(257)));
        make.height.equalTo(@(1));
    }];
    [self.passwordFieldIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(20)));
        make.height.equalTo(@(RPX(22)));
        make.right.equalTo(self.passwordFieldBottomLine);
        make.centerY.equalTo(self.passwordField);
    }];
    [self.forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(RPX(13)));
        make.top.equalTo(self.view).offset(RPX(368));
        make.left.equalTo(self.view).offset(RPX(31));
    }];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(RPX(13)));
        make.top.equalTo(self.view).offset(RPX(368));
        make.left.equalTo(self.view).offset(RPX(180));
    }];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(444));
        make.left.equalTo(self.view).offset(RPX(58));
        make.width.equalTo(@(RPX(150)));
        make.height.equalTo(@(RPX(152)));
    }];
    [self.protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(RPX(11)));
        make.bottom.equalTo(self.view).offset(RPX(-20));
        make.right.equalTo(self.view).offset(RPX(-20));
    }];
}
- (void)refreshSelf {
}
@end
